package com.tg.app.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tg.app.R;
import com.tg.app.helper.ActivityHelper;

import com.tg.appcommon.android.FontUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;


public class AlertStandardVoiceView extends RelativeLayout {
    public static final int SIZE = 14;
    private static final String TAG = AlertStandardVoiceView.class.getSimpleName();
    private static final int TEXT_MAX_LENGTH = 10;
    private static final int TEXT_MIN_LENGTH = 1;
    private static final int VOICE_MALE = 0;
    private static final int VOICE_FEMALE = 1;
    private TextView mTvNumber;
    private EditText mEditText;
    private int mSelectVoice = VOICE_MALE;
    private Button mButton;
    private CheckBox mCheckBoxMale, mCheckBoxFeMale;
   //// private final TtsHelper mTtsHelper = new TtsHelper();

    private String mFrom;
    public Button getButton() {
        return mButton;
    }

    public void setFrom(String from){
        mFrom = from;
      //  mTtsHelper.setFrom(from);
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
   //     mTtsHelper.destroy();
    }

    public AlertStandardVoiceView(Context context) {
        super(context);
        initContentView(context);
    }

    public AlertStandardVoiceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AlertStandardVoiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initContentView(context);
    }

    private void initContentView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.activity_custom_alert_sound_standard, this, true);
        mEditText = rootView.findViewById(R.id.et_text);
        FontUtil.SetEditTextHintFontSize(mEditText, AlertStandardVoiceView.SIZE, R.string.txt_input_warnning_voice);
        mTvNumber = rootView.findViewById(R.id.tv_input_number);
        setInputTextNumber(0);
        mButton = rootView.findViewById(R.id.login);
        ActivityHelper.setButtonEnable(mButton, false);
        mCheckBoxMale = rootView.findViewById(R.id.check_box_male_voice);
        mCheckBoxFeMale = rootView.findViewById(R.id.check_box_female_voice);
        //mTtsHelper.buildTts(context);
        setListener(rootView);
    }

    private void setListener(View rootView) {
        mCheckBoxMale.setOnClickListener(v -> {
            mSelectVoice = VOICE_MALE;
            mCheckBoxMale.setChecked(true);
            mCheckBoxFeMale.setChecked(false);

        });

        mCheckBoxFeMale.setOnClickListener(v -> {
            mSelectVoice = VOICE_FEMALE;
            mCheckBoxFeMale.setChecked(true);
            mCheckBoxMale.setChecked(false);
        });

        rootView.findViewById(R.id.image1).setOnClickListener(v -> playOrSave(v.getContext(), false));

        setEditListener();
    }

    public boolean playOrSave(Context context, boolean save){
        String text = mEditText.getText().toString().trim();
        TGLog.d(TAG + "=" + text);
        if (!StringUtils.isEmpty(text)){
//            String voice = mSelectVoice == VOICE_MALE ? TtsHelper.NAME_MALE : TtsHelper.NAME_FEMALE;
//            if (save){
//                return mTtsHelper.save(context, text, voice);
//            }else{
//                mTtsHelper.startPlay(context, text, voice);
//            }
        }
        return true;
    }

    private void setEditListener() {
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    ActivityHelper.setButtonEnable(mButton, false);
                } else {
                    String text = s.toString();
                    if (TextUtils.isEmpty(text)) {
                        ActivityHelper.setButtonEnable(mButton, false);
                    } else {
                        int len = text.length();
                        setInputTextNumber(len);
                        ActivityHelper.setButtonEnable(mButton, len >= TEXT_MIN_LENGTH);
                    }
                }
            }
        });
    }

    private void setInputTextNumber(int number) {
        String text = number + "/" + TEXT_MAX_LENGTH;
        mTvNumber.setText(text);
    }

}
